#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "steering.h"
//#include "usmart.h"
#include "key.h"
#include "motor.h"
#include "find.h"
extern u8 Res;
extern int bt;
	
int main(void)
 {

  //bt=100;
	KEY_Init();
	delay_init(168);
  u32 i=0;
	uart_init(115200);
	Find_IO_Init();	 //这个就是串口初始化(库文件可以不用动)    初始化串口波特率为115200 
 	TIM4_PWM_Init(600-1,2800-1);	 //  
	TIM2_PWM_Init(200-1,8400-1);
	MOTOR_GPIO_Init();
	//u16 panduan=0;
	TIM3_PWM_Init(200-1,8400-1);
	 
	u16 pwmval_1=35;                        //这几行都是调舵机的不用管
  u16 pwmval_2=35;
	u16 pwmval_3=35;

	u16 pwmval_5=15;
	u16 pwm_cargo = 150;//开始是全速运动
	 

	TIM_SetCompare1(TIM4,pwmval_1);
	TIM_SetCompare2(TIM4,pwmval_2);
	TIM_SetCompare3(TIM4,pwmval_3);

	
  TIM_SetCompare5(TIM2,pwmval_5);
	
	TIM_SetCompare6(TIM3,pwm_cargo);
	TIM_SetCompare7(TIM3,pwm_cargo);
	TIM_SetCompare8(TIM3,pwm_cargo);
	TIM_SetCompare9(TIM3,pwm_cargo);
while(1) 
{   

  switch(bt)
	 {
		 case 0:
			 {
				 pwmval_1 =pwmval_1+1 ;                        //通过改变pwm占空比师舵机转向
		 		if(pwmval_1>75)pwmval_1= 75;
		 	TIM_SetCompare1(TIM4,pwmval_1);
				 for(i=0;i<1000000;i++);
				 break;
			 }
		case 1:
			{
				pwmval_1 =pwmval_1-1;
					if(pwmval_1<15)
						pwmval_1 = 15;
			TIM_SetCompare1(TIM4,pwmval_1);
					 for(i=0;i<1000000;i++);
					break;
			}
		case 2:	 
			{
			pwmval_2 =pwmval_2+1 ;
		 	if(pwmval_2>75)
				pwmval_2= 75;
		 		TIM_SetCompare2(TIM4,pwmval_2);
			 for(i=0;i<1000000;i++);
			break;
			}
		case 3:
			{
			pwmval_2 =pwmval_2-1;
		 	if(pwmval_2<15)
				pwmval_2 = 15;
				TIM_SetCompare2(TIM4,pwmval_2);
			 for(i=0;i<1000000;i++);
			break;
		}
		case 4: 
			{
				pwmval_3 =pwmval_3+1 ;
		 if(pwmval_3>75)pwmval_3= 75;
		 TIM_SetCompare3(TIM4,pwmval_3);
				 for(i=0;i<1000000;i++);
				break;
			}
		case 5:	{
			pwmval_3 =pwmval_3-1;
			if(pwmval_3<15)pwmval_3 = 15;
			TIM_SetCompare3(TIM4,pwmval_3);
			 for(i=0;i<1000000;i++);
			break;
		}
		case 6: {
			pwmval_5 =20 ;
		 	if(pwmval_5>25)pwmval_5= 25;
		 		TIM_SetCompare5(TIM2,pwmval_5);
			break;
		}
		case 7: {
			pwmval_5 =10 ;
		 		TIM_SetCompare5(TIM2,pwmval_5);
			break;
		}
			case 8:	 {
				pwmval_5 =25 ;
		 		TIM_SetCompare5(TIM2,pwmval_5);
				break;
			}
		 case 9: {
			 pwmval_5 =5 ;
		 		TIM_SetCompare5(TIM2,pwmval_5);
			 break;
		 }
			case 10: {
				pwmval_5 =15 ;
		 		TIM_SetCompare5(TIM2,pwmval_5);
				break;
			}
			case 11:	           //蓝牙接收信号，遥控小车
			{Car_Go();
				break;
			}
			case 12:	
			{Car_Back();
				break;}
			case 13:
			{Car_Left();
				break;}
			case 14:	
			{Car_Right();
				break;}
			case 15:
			{pwm_cargo=200;
				break;}
			case 16:	
			{pwm_cargo=150;
				break;}
			case 17:
			{Car_Stop();
			break;}
			case 18:
			{Find ();
			break;}
			case 19:
			{Car_Turn_Left();
			break;}
			case 20:
			{Car_Turn_Right();
			break;}

	 	 }	
}
}
